package org.su.webadapter.bind;

import org.apache.commons.lang.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;


public class DateEditor extends AbstractParameterEditor {
	private final DateFormat dateFormat;

	public DateEditor(DateFormat dateFormat) {
		this.dateFormat = dateFormat;
	}

	public DateEditor() {
		dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
	}

	public Object parseText(String text) {
		Object result = null;
		if (StringUtils.isNotBlank(text)) {
			try {
				result = this.dateFormat.parse(text);
			} catch (ParseException e) {
				throw new ParamBindException(e);
			}
		}
		return result;
	}
}
